package com.zrgj.services;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zrgj.entity.Goods;
import com.zrgj.entity.Type;
import com.zrgj.entity.Unit;
import com.zrgj.entity.WmType;
import com.zrgj.mapper.GoodsMapper;
import com.zrgj.util.HanyuPinyinHelper;
import com.zrgj.vo.GoodsVO;
import com.zrgj.vo.Pinyin;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
public class GoodsServiceImpl{

    @Resource
    private GoodsMapper goodsMapper;


    public int save(Goods goods) throws BadHanyuPinyinOutputFormatCombination {

        String uuid= UUID.randomUUID().toString().replace("-","");
        goods.setId(uuid);
        SimpleDateFormat sdf = new SimpleDateFormat( " yyyy-MM-dd HH:mm:ss " );
        String str = sdf.format(new Date());
        String pinyin= HanyuPinyinHelper.getPinyinString(goods.getName());
        goods.setPinyin(pinyin);
        goods.setStatus(1);
        goods.setCreateTime(str);
        goods.setModifyTime(str);
        Integer num1=goodsMapper.show(goods);
        if (num1>0){
            int num=0;
            return num;
        }else{

            return goodsMapper.save(goods);

        }


    }



    public int delById(Goods goods) {

        goods.setStatus(0);

        return goodsMapper.delById(goods);
    }

    public int deleteByIds(String[] ids) throws Exception{
        return goodsMapper.deleteByIds(ids);
    }


    public Goods queryById(Goods goods) {
        return goodsMapper.queryById(goods);
    }




    public List<Goods> list(Goods goods) {


        return goodsMapper.list(goods);
    }


    public List<GoodsVO> listVO(Goods goods) {
        String keyword=goods.getKeyword();
        if (keyword!=null&&!"".equals(keyword)){
            keyword="%"+keyword+"%";
            goods.setKeyword(keyword);
        }


        return goodsMapper.listVO(goods);
    }


    public PageInfo queryPyCdName(GoodsVO goodsVO) {
        Page page= PageHelper.startPage(goodsVO.getPageNum(), goodsVO.getPageSize(), true);
        String keyword=goodsVO.getKeyword();
        if (keyword!=null&&!"".equals(keyword)){
            keyword="%"+keyword+"%";
            goodsVO.setKeyword(keyword);
        }
        List list=goodsMapper.queryPyCdName(goodsVO);
        //pagehelper内置的类
        PageInfo<Goods> pageInfo=new PageInfo<Goods>(list,page.getPages());



        return pageInfo;
    }


    public List<Goods> queryExcel(Goods goods) throws Exception{

        return goodsMapper.queryExcel(goods);
    }

    public List<Type> queryType(Type type) throws Exception{

        return goodsMapper.queryType(type);
    }


    public List<Unit> queryUnit(Unit unit) throws Exception{

        return goodsMapper.queryUnit(unit);
    }

    public List<WmType> queryWmType(WmType wmType) throws Exception{

        return goodsMapper.queryWmType(wmType);
    }



    public int modifyById(Goods goods) {

        return goodsMapper.modifyById(goods);
    }
}
